package org.example.lanhang.service.impl;

import org.example.lanhang.VO.UserVO;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.lanhang.entity.user;
import org.example.lanhang.mapper.UserMapper;
import org.example.lanhang.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.lanhang.util.PageResult;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gww
 * @since 2025-01-07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, user> implements IUserService {

    @Override
    public PageResult<user> findByPage(Integer page, Integer pageSize) {
        Page<user>userPage=this.page(new Page<>(page,pageSize));
        return new PageResult<>(userPage.getRecords(),userPage.getTotal());
    }

    @Override
    public PageResult<user> search(Integer page, Integer pageSize, UserVO userVO) {
        QueryWrapper<user>wrapper=new QueryWrapper<>();
//        wrapper.eq(userVO.getId() != null && userVO.getId() != 0, "id", userVO.getId());
        wrapper.like(StrUtil.isNotBlank(userVO.getName()),"name",userVO.getName());
        wrapper.eq(userVO.getAge() != null && userVO.getAge() != 0, "age", userVO.getAge());
        Page<user>userPage = this.page(new Page<>(page, pageSize), wrapper);
        return new PageResult<>(userPage.getRecords(),userPage.getTotal());

    }

    @Override
    public boolean add(user user) {
        return this.save(user);
    }
}
